package com.xjcs.saplingplan.ui.activity.countdown

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.activity.viewModels
import com.xjcs.saplingplan.R
import com.xjcs.saplingplan.base.BaseVbActivity
import com.xjcs.saplingplan.const.CountdownRepeatType
import com.xjcs.saplingplan.const.setSingleClick
import com.xjcs.saplingplan.const.timeMillis
import com.xjcs.saplingplan.const.toColor
import com.xjcs.saplingplan.const.toStr
import com.xjcs.saplingplan.const.toToast
import com.xjcs.saplingplan.databinding.ActivityCountdownCreateBinding
import com.xjcs.saplingplan.datastore.AppData
import com.xjcs.saplingplan.dialog.CountdownCatePop
import com.xjcs.saplingplan.dialog.CountdownDatePop
import com.xjcs.saplingplan.dialog.RemindPop
import com.xjcs.saplingplan.room.bean.countdown.CountdownBean
import com.xjcs.saplingplan.ui.viewmodel.CountdownViewModel
import com.xjcs.saplingplan.utils.DialogUtils
import java.time.LocalDate
import java.util.UUID

class CountdownCreateActivity : BaseVbActivity<ActivityCountdownCreateBinding>() {

    companion object {
        fun startActivity(context: Context, countdownType: Int) {
            val intent = Intent(context, CountdownCreateActivity::class.java)
            intent.putExtra("countdownType", countdownType)
            context.startActivity(intent)
        }
    }

    private val userId = AppData.getUserId()

    private val mViewModel: CountdownViewModel by viewModels()

    private val curDate = LocalDate.now()

    private val countDownBean = CountdownBean().apply {
        countdownId = UUID.randomUUID().toString()
        countdownUserId = userId
        countdownDateMillis = curDate.timeMillis()
        countdownRemindDays = "0"
        countdownRemind = "08:00"
    }

    private val colorF7F7F7 = "#F7F7F7".toColor()
    private val color2034C759 = "#2034C759".toColor()

    override fun getViewBinding(): ActivityCountdownCreateBinding {
        return ActivityCountdownCreateBinding.inflate(layoutInflater)
    }

    override fun initUi(savedInstanceState: Bundle?) {
        countDownBean.countdownType = intent.getIntExtra("countdownType", 0)
        mBinding.tvTitle.text = "新增" + countDownBean.getTypeStr()
    }

    override fun registerObserver() {
        mViewModel.countdownCateList.observe(this) {
            DialogUtils.showBasePop(this, outDismiss = true, showBg = true)
                .asCustom(CountdownCatePop(this, countDownBean.countdownCateName, it) { cate ->
                    countDownBean.countdownCateName = cate
                    mBinding.tvCateName.text = ""
                })
        }
    }

    override fun initListener() {
        mBinding.ivBack.setSingleClick {
            finish()
        }

        //选择目标日
        mBinding.linearDate.setSingleClick {
            showDatePop()
        }

        //选择重复周期
        mBinding.tvNone.setSingleClick {
            refreshRepeatUi(0)
        }
        mBinding.tvWeek.setSingleClick {
            refreshRepeatUi(1)
        }
        mBinding.tvMonth.setSingleClick {
            refreshRepeatUi(2)
        }
        mBinding.tvYear.setSingleClick {
            refreshRepeatUi(3)
        }

        //选择提醒时间
        mBinding.linearRemind.setSingleClick {
            DialogUtils.showBasePop(this, outDismiss = true, showBg = true)
                .asCustom(RemindPop(this, countDownBean.countdownRemindDays, countDownBean.countdownRemind) { remindDays, remindTime ->
                    countDownBean.countdownRemindDays = remindDays
                    countDownBean.countdownRemind = remindTime
                    mBinding.tvRemind.text = countDownBean.getRemindStr()
                }).show()
        }

        //选择分类
        mBinding.linearCate.setSingleClick {
            mViewModel.getCountdownCateList(userId)
        }

        //选择背景
        mBinding.linearBg.setSingleClick {
            CountdownBgActivity.startActivity(this)
        }

        //保存
        mBinding.tvSave.setSingleClick {
            val title = mBinding.etTitle.text.toString().trim()
            if (title.isEmpty()) {
                "请输入事件名称".toToast()
                return@setSingleClick
            }
            if (countDownBean.countdownDate.isEmpty()) {
                "请选择目标日".toToast()
                return@setSingleClick
            }
            countDownBean.countdownTitle = title
            mViewModel.insertCountdownBean(countDownBean)
            finish()
        }
    }

    private fun showDatePop() {
        val selectDate = countDownBean.countdownDate.ifEmpty { LocalDate.now().toStr() }
        DialogUtils.showBasePop(this, outDismiss = true, showBg = true)
            .asCustom(
                CountdownDatePop(
                    this,
                    countDownBean.isLunar(),
                    selectDate,
                    countDownBean.isBirthday(),
                    countDownBean.countHiddenYear
                ) { hiddenYear, isLunar, dateFormat ->
                    val date = LocalDate.parse(dateFormat)
                    countDownBean.apply {
                        countHiddenYear = hiddenYear
                        countdownDateType = if (isLunar) "lunar" else "solar"
                        countdownDateMillis = date.timeMillis()
                        countdownDate = date.toString()
                        mBinding.tvDate.text =
                            formatSelectDate(countdownDateMillis, this)
                    }
                })
            .show()
    }

    private fun refreshRepeatUi(type: Int){
        countDownBean.apply {
            when(countdownRepeatType){
                CountdownRepeatType.NONE.value -> {
                    mBinding.tvNone.setBgColor(colorF7F7F7)
                    mBinding.tvNone.setTextColor(Color.BLACK)
                }
                CountdownRepeatType.WEEK.value -> {
                    mBinding.tvWeek.setBgColor(colorF7F7F7)
                    mBinding.tvWeek.setTextColor(Color.BLACK)
                }
                CountdownRepeatType.MONTH.value -> {
                    mBinding.tvMonth.setBgColor(colorF7F7F7)
                    mBinding.tvMonth.setTextColor(Color.BLACK)
                }
                CountdownRepeatType.YEAR.value -> {
                    mBinding.tvYear.setBgColor(colorF7F7F7)
                    mBinding.tvYear.setTextColor(Color.BLACK)
                }
            }
            countdownRepeatType = type
            when(countdownRepeatType){
                CountdownRepeatType.NONE.value -> {
                    mBinding.tvNone.setBgColor(color2034C759)
                    mBinding.tvNone.setTextColor(getColor(R.color.color_34C759))
                }
                CountdownRepeatType.WEEK.value -> {
                    mBinding.tvWeek.setBgColor(color2034C759)
                    mBinding.tvWeek.setTextColor(getColor(R.color.color_34C759))
                }
                CountdownRepeatType.MONTH.value -> {
                    mBinding.tvMonth.setBgColor(color2034C759)
                    mBinding.tvMonth.setTextColor(getColor(R.color.color_34C759))
                }
                CountdownRepeatType.YEAR.value -> {
                    mBinding.tvYear.setBgColor(color2034C759)
                    mBinding.tvYear.setTextColor(getColor(R.color.color_34C759))
                }
            }

            mBinding.tvDate.text =
                formatSelectDate(countdownDateMillis, this)
        }
    }

}